<?php

class Wlib_View_Helper_MultiDynamic extends Zend_Dojo_View_Helper_Dijit
{
    protected $_dijit  = null;

    protected $_elementType = 'text';

    protected $_module = null;

    public function multiDynamic($id, $value = null, array $params = array(), array $attribs = array())
    {
        foreach (array('data', 'deleted', 'store') as $key) {
            $value[$key] = isset($value[$key]) ? $value[$key] : array();
        }

        $rowsValues = $this->convertValue($value['data']);
        $storeValues = $this->convertValue($value['store']);
        $deletedValues = $this->convertValue($value['deleted']);

        $params['rowsValues'] = $rowsValues;
        $params['deletedValues'] = $deletedValues;
        $params['baseName'] = $id;

        $value = '';

        return $this->_createFormElement($id, $value, $params, $attribs);
    }

    public function convertValue($value)
    {
        if(!$value) {
            $value = array();
        }

        if(!is_string($value)) {
            $value = Zend_Json::encode($value);
        }

        return $value;
    }
}
